package com.mcarthur.countdown;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;


public class DigitGroup extends AbstractDrawable {
	private static final int DIGIT_SEP = 23;
	private Digit[] digits;

	public DigitGroup(float posX, int numDigits, Color activeColor) {
		super(posX);
		digits = new Digit[numDigits];
		float currentX = posX;
		for (int i = 0; i < numDigits; i++) {
			digits[i] = new Digit(currentX, activeColor);
			currentX += Digit.WIDTH + DIGIT_SEP;
		}
	}

	@Override
	public void draw(GL10 gl) {
		for (int i = 0; i < digits.length; i++) {
			digits[i].draw(gl);
		}
	}
	
	/**
	 * Sets the number rendered by this digit group.
	 * 
	 * @param number the number to render
	 */
	public void setNumber(int number) {
		String numberString = String.valueOf(number);
		int digitIndex = digits.length - numberString.length();
		int stringIndex = 0;
		
		for (int i = 0; i < digitIndex; i++) {
			// set unused digits to 0
			digits[i].setDigit('0');
		}
		for (int i = digitIndex; i < digits.length; i++) {
			digits[i].setDigit(numberString.charAt(stringIndex++));
		}
	}
	
	@Override
	public void setPosY(float posY) {
		super.setPosY(posY);
		for (Digit digit : digits) {
			digit.setPosY(posY);
		}
	}
	
	public int getTotalWidth() {
		return digits.length * (Digit.WIDTH + DigitGroup.DIGIT_SEP) - DigitGroup.DIGIT_SEP;
	}
}
